﻿using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

namespace LazyFramework.Game
{
    public class EntitySystemManager : Singleton<EntitySystemManager>
    {
        private NativeList<Entity> entities = new NativeList<Entity>(Allocator.Temp);
        private EntityManager entityManager = null;

        public Entity CreateEntity(EntityArchetype archetype)
        {
            if (entityManager == null)
            {
                entityManager = World.Active.EntityManager;
            }
            var entity = entityManager.CreateEntity(archetype);
            entities.Add(entity);
            return entity;
        }

        public T CreateComponentSystemBase<T>(string type) where T : ComponentSystemBase, new()
        {
            T tInstance = (T)World.Active.CreateSystem(Type.GetType(type, true));
            return tInstance;
        }

        public void DeleteEntity(Entity entity)
        {
            if (entityManager == null)
            {
                entityManager = World.Active.EntityManager;
            }
            if (entityManager.Exists(entity))
            {
                entityManager.DestroyEntity(entity);
            }
        }
    }

}

